package com.meiyu.live.ui.main;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.AppCompatRadioButton;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.android.volley.Response;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.meiyu.live.BaseActivity;
import com.meiyu.live.R;
import com.meiyu.live.comm.Constant;
import com.meiyu.live.net.param.BaseBodyParams;
import com.meiyu.live.net.response.FocusResponse;
import com.meiyu.live.popj.Assort;
import com.meiyu.live.tv.widget.AssortSelectDialog;
import com.meiyu.live.util.ToastUtil;
import com.meiyu.live.widget.NetLoadingDailog;
import com.meiyu.live.widget.StartEndTimeDialog;

import java.text.SimpleDateFormat;
import java.util.Calendar;

/**
 * 主播资料
 * Created by zbl on 2016/9/22.
 */
public class Apply1Activity extends BaseActivity implements View.OnClickListener {

    private static final int REQUEST_CODE_APPLY2 = 2;

    private NetLoadingDailog dialog_loading;
    private StartEndTimeDialog dialog_datetime;
    private AssortSelectDialog assortSelectDialog;
    private ImageLoader imageLoader;
    private DisplayImageOptions options;

    private View btn_back;
    private TextView btn_next;
    private TextView tv_time_select, tv_column;
    private EditText et_price;
    private AppCompatRadioButton rb_type_public, rb_type_private;

    private Calendar calendarStart, calendarEnd;
    private Assort assortBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_apply1);
        init();
        getAllAssorts();
    }

    private void init() {
        imageLoader = ImageLoader.getInstance();
        options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.drawable.image_loading)          // 设置图片下载期间显示的图片
                .showImageForEmptyUri(R.drawable.image_loading)  // 设置图片Uri为空或是错误的时候显示的图片
                .showImageOnFail(R.drawable.image_loading)       // 设置图片加载或解码过程中发生错误显示的图片
                .cacheInMemory(true)                        // 设置下载的图片是否缓存在内存中
                .cacheOnDisk(true)                          // 设置下载的图片是否缓存在SD卡中
                .displayer(new FadeInBitmapDisplayer(200))// 图片加载好后渐入的动画时间
                .build();
        dialog_loading = new NetLoadingDailog(this);
        assortSelectDialog = new AssortSelectDialog(this);
        assortSelectDialog.setListener(new AssortSelectDialog.AssortSelectedListener() {
            @Override
            public void onAssortSelected(Assort assort) {
                if (assort == null) {
                    assortBean = null;
                    tv_column.setText("请选择分类");
                } else {
                    assortBean = assort;
                    tv_column.setText(assort.getName());
                }
            }
        });

        btn_back = findViewById(R.id.btn_back);
        btn_back.setOnClickListener(this);
        btn_next = (TextView) findViewById(R.id.btn_next);
        btn_next.setOnClickListener(this);

        rb_type_public = (AppCompatRadioButton) findViewById(R.id.rb_type_public);
        rb_type_private = (AppCompatRadioButton) findViewById(R.id.rb_type_private);
        rb_type_private.setChecked(true);

        tv_time_select = (TextView) findViewById(R.id.tv_time_select);
        tv_time_select.setOnClickListener(this);
        tv_column = (TextView) findViewById(R.id.tv_column);
        tv_column.setOnClickListener(this);
        et_price = (EditText) findViewById(R.id.et_price);
    }

    @Override
    public void onClick(View view) {
        if (view == btn_back) {
            finish();
        } else if (view == btn_next) {
            if (calendarStart == null || calendarEnd == null) {
                ToastUtil.show("请选择开始和结束日期时间");
                return;
            } else if (assortBean == null) {
                ToastUtil.show("请选择分类");
                return;
            }
            //总认购份额
            int price = 0;
            try {
                price = Integer.getInteger(et_price.getText().toString(), 0);
            } catch (Exception e) {
                e.printStackTrace();
            }
            //公开or私密
            int channel_status = 1;
            if (rb_type_public.isChecked()) {
                channel_status = 0;
            }
            //开始结束日期
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            String startTimeText = format.format(calendarStart.getTimeInMillis());
            String endTimeText = format.format(calendarEnd.getTimeInMillis());
            Intent intent = new Intent(this, Apply2Activity.class);
            intent.putExtra(Apply2Activity.DATA_TOTAL_SHARES, price);
            intent.putExtra(Apply2Activity.DATA_CHANNEL_STATUS, channel_status);
            intent.putExtra(Apply2Activity.DATA_START_TIME, startTimeText);
            intent.putExtra(Apply2Activity.DATA_END_TIME, endTimeText);
            intent.putExtra(Apply2Activity.DATA_ASSORT_ID, (int) assortBean.getId());
            startActivityForResult(intent, REQUEST_CODE_APPLY2);
        } else if (view == tv_time_select) {
            if (dialog_datetime == null) {
                dialog_datetime = new StartEndTimeDialog(this);
                dialog_datetime.setDatePickerDialogListener(new StartEndTimeDialog.DatePickerDialogListener() {
                    @Override
                    public void onSelectTime(Calendar start, Calendar end) {
                        calendarStart = start;
                        calendarEnd = end;
                        SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日 HH:mm");
                        String startTimeText = format.format(start.getTimeInMillis());
                        String endTimeText = format.format(end.getTimeInMillis());
                        tv_time_select.setText(startTimeText + " 到 \n" + endTimeText);
                    }
                });
            }
            dialog_datetime.show();
        } else if (view == tv_column) {
            assortSelectDialog.show();
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }

    @Override
    public void onError() {
        super.onError();
        dialog_loading.dismissDialog();
    }

    private void getAllAssorts() {
        dialog_loading.loading();
        request(new BaseBodyParams(Constant.UrlParams.FOCUS), FocusResponse.class, new Response.Listener<FocusResponse>() {
            @Override
            public void onResponse(FocusResponse focusResponse) {
                dialog_loading.dismissDialog();
                if (focusResponse != null) {
                    assortSelectDialog.setData(focusResponse.getAll_assorts());
                }
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE_APPLY2 && resultCode == RESULT_OK) {
            finish();
        }
    }
}
